/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewOkCancelDialog.java
 *
 * Created on 23-Apr-2011, 6:26:36 PM
 */

package pos.ui.screen;

import java.util.ArrayList;
import java.util.ListIterator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pos.entity.PriceAdjustment;
import pos.entity.PriceAdjustment.PriceAdjustmentType;

/**
 *
 * @author Justin Hume
 */
public class CustomerDiscountDialog extends javax.swing.JDialog {
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;

    //Having the customer screen
    Customer customer = null;
    
    //Private fields
    private PriceAdjustment _discount;
    private double _basePrice;

    //Constructor
    public CustomerDiscountDialog(java.awt.Frame parent, boolean modal, PriceAdjustment discount, String itemName, double totalPriceExclDiscounts) {

        super(parent, modal);
        setSize(530, 450);
        initComponents();


        //Set frame properties
        _discount = discount;
        _basePrice = totalPriceExclDiscounts;

        //Set frame text fields and text areas
        txtItemName.setText(itemName);
        txtPurchasedItemId.setText("" + discount.getPriceAdjustmentId());
        txaDiscountDescription.setText(discount.getPriceAdjustmentDescription());

        //Calculate total price
        double totalPrice = totalPriceExclDiscounts;

        //Set the text for the current total price, and the derived price
        txtCurrTotalPrice.setText("" + totalPrice);
        txtTotalPrice.setText("" + totalPrice);

        //Add discount selection combo box values
        cmbAddDiscount.addItem(0);
        cmbAddDiscount.addItem(5);
        cmbAddDiscount.addItem(10);
        cmbAddDiscount.addItem(15);
        cmbAddDiscount.addItem(20);
        cmbAddDiscount.addItem(25);

        //Add discount type combo box
        cmbSelectDiscountType.setModel(new DefaultComboBoxModel(PriceAdjustmentType.values()));

        //Set form fields to NOT enabled for editing
        this.txtCurrTotalPrice.setEnabled(false);
        this.txtAddedDiscount.setEnabled(false);
        this.txtItemName.setEnabled(false);
        this.txtPurchasedItemId.setEnabled(false);
        this.txtTotalPrice.setEnabled(false);
    }

    public void setCustomer(Customer customer){
        this.customer = customer;
    }
    
    public Customer getCustomer(){
        return customer;
    }

    //Calculate the total price on this form
    private double calculateTotalItemPrice(double currBasePrice, double addedDiscount) {

        double totalPrice = currBasePrice - addedDiscount;

        return totalPrice;
    }

    //Determine if all required input fields have valid values
    private boolean fieldsAreValid(boolean giveMessageToUser) {

        boolean allFieldsAreValid = true;

        //Create a list of messages for each invalid field
        ArrayList<String> invalidFieldMessageList = new ArrayList<String>();

        //Check that all fields are non-empty and otherwise valid
        if (txaDiscountDescription.getText().length() == 0) {

            allFieldsAreValid = false;
            invalidFieldMessageList.add("Enter a promotion description");
        }

        if (Double.parseDouble(this.txtTotalPrice.getText()) < 0) {

            allFieldsAreValid = false;
            invalidFieldMessageList.add("Total discounted price must be greater than 0");
        }

        //Give the user a message if this is required
        if (giveMessageToUser && !invalidFieldMessageList.isEmpty())
        {
            //Create a string of
            String invalidString = "";
            ListIterator<String> invalidListIter = invalidFieldMessageList.listIterator();

            //Concatenate each message. Each message should be on a new line, hence
            //the new line character
            while (invalidListIter.hasNext()) {

                invalidString = invalidString + invalidListIter.next() + "\n";
            }

            //Display the message
            JOptionPane.showMessageDialog(this, invalidString, "Invalid data", JOptionPane.ERROR_MESSAGE);
        }

        return allFieldsAreValid;
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        txtItemName = new javax.swing.JTextField();
        txtTotalPrice = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        txtCurrTotalPrice = new javax.swing.JTextField();
        txtPurchasedItemId = new javax.swing.JTextField();
        btnOK = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cmbAddDiscount = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDiscountDescription = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        txtAddedDiscount = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        cmbSelectDiscountType = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(520, 450));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Total Price");

        txtItemName.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        txtTotalPrice.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Purchase Id");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Total Discounted Price");

        txtCurrTotalPrice.setFont(new java.awt.Font("Tahoma", 0, 12));

        txtPurchasedItemId.setFont(new java.awt.Font("Tahoma", 0, 12));

        btnOK.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnOK.setText("OK");
        btnOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnOKMouseClicked(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Discount Description");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Add discount (%)");

        cmbAddDiscount.setFont(new java.awt.Font("Tahoma", 0, 12));
        cmbAddDiscount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbAddDiscountActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Item Name");

        txaDiscountDescription.setColumns(20);
        txaDiscountDescription.setRows(5);
        jScrollPane1.setViewportView(txaDiscountDescription);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("Additional discount");

        btnCancel.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnCancel.setText("Cancel");
        btnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelMouseClicked(evt);
            }
        });

        txtAddedDiscount.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Add Discount to Item");

        cmbSelectDiscountType.setFont(new java.awt.Font("Tahoma", 0, 12));
        cmbSelectDiscountType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbSelectDiscountTypeActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Discount Type");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(txtPurchasedItemId, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(txtItemName, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                .addGap(40, 40, 40))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCurrTotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbAddDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbSelectDiscountType, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAddedDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(87, 87, 87)
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1)
                .addGap(7, 7, 7)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(txtPurchasedItemId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(txtItemName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(105, 105, 105)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6)
                        .addComponent(txtCurrTotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(cmbAddDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(10, 10, 10)
                                .addComponent(cmbSelectDiscountType, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(4, 4, 4)
                                .addComponent(txtAddedDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)
                                .addComponent(txtTotalPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void btnOKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnOKMouseClicked

        if (fieldsAreValid(true)) {

            //Set price adjustment description
            _discount.setPriceAdjustmentDescription(txaDiscountDescription.getText());

            //Set discount value on object class
            double currTotalPrice = _basePrice;
            double newDiscountValue = currTotalPrice * (Integer)cmbAddDiscount.getSelectedItem()/100;
            _discount.setPriceAdjustmentAmount(newDiscountValue);

            //Set the discount type
            _discount.setPriceAdjustmentType((PriceAdjustmentType)cmbSelectDiscountType.getSelectedItem());

            doClose(RET_OK);
        }
    }//GEN-LAST:event_btnOKMouseClicked

    private void cmbAddDiscountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbAddDiscountActionPerformed

        //Get discount value and calculate new final price
        double currTotalPrice = _basePrice;
        double newDiscountValue = currTotalPrice * (Integer)cmbAddDiscount.getSelectedItem()/100;

        //Calculate final price and input
        double totalPrice = calculateTotalItemPrice(
                _basePrice,
                newDiscountValue);


        txtAddedDiscount.setText("" + newDiscountValue);
        txtTotalPrice.setText("" + totalPrice);
    }//GEN-LAST:event_cmbAddDiscountActionPerformed

    private void btnCancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelMouseClicked
        this.doClose(RET_CANCEL);
}//GEN-LAST:event_btnCancelMouseClicked

    private void cmbSelectDiscountTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbSelectDiscountTypeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbSelectDiscountTypeActionPerformed

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JComboBox cmbAddDiscount;
    private javax.swing.JComboBox cmbSelectDiscountType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea txaDiscountDescription;
    private javax.swing.JTextField txtAddedDiscount;
    private javax.swing.JTextField txtCurrTotalPrice;
    private javax.swing.JTextField txtItemName;
    private javax.swing.JTextField txtPurchasedItemId;
    private javax.swing.JTextField txtTotalPrice;
    // End of variables declaration//GEN-END:variables

    private int returnStatus = RET_CANCEL;
}
